package com.lagou.demo

class Bottle {
  val cost = 0;
  val RATIO = 3

  def canExchange(bottles: Int): Boolean = {
    bottles >= RATIO
  }

  def doExchange(bottles: Int): (Int, Int) = {
    val last = bottles % RATIO
    val add: Int = math.floor(bottles / RATIO).toInt

    (add, last)
  }
}

class Cap {
  val cost = 0;
  val RATIO = 5

  def canExchange(caps: Int): Boolean = {
    caps >= RATIO
  }

  def doExchange(caps: Int): (Int,Int) = {
    val last = caps % RATIO
    val add: Int = math.floor(caps / RATIO).toInt
    (add, last)
  }
}


object MaxBeers {

  val bottleMgr = new Bottle;
  def getBottleMgr(): Bottle = {
    bottleMgr;
  }

  val capMgr = new Cap;
  def getCapMgr(): Cap = {
    capMgr;
  }

  def exchangeBeer(bottle: Int, cap: Int, drink: Int): Int = {
    printf("已喝 %d 瓶，有盖 %d个，有空瓶 %d 个\n",drink, cap, bottle)

    var add = 0;
    if(!this.getBottleMgr().canExchange((bottle)) && ! this.getCapMgr().canExchange(cap))
      add = 0;
    else {
      val (add1, left1) = this.getBottleMgr().doExchange(bottle);
      val (add2, left2) = this.getCapMgr().doExchange(cap);
      printf("用空瓶兑换了 %d 瓶，有盖子兑换了 %d 瓶， 现剩余瓶 %d 个，剩余盖 %d个\n",add1, add2, left1, left2)
      add = exchangeBeer(add1 + add2 + left1, add1 + add2 + left2, add1 + add2 )
    }

    drink + add;
  }

  def main(args: Array[String]): Unit = {
    //单价
    val price = 2
    //总钱数
    val money = 100

    //不允许借啤酒，一次性买完
    val buy = money / price;

    val total = exchangeBeer(buy, buy, buy)

    printf("一共喝了 %d 瓶\n", total)
  }
}
